import { Controller } from "../../mvc/core/Controller";
import { IFightSpiritModule } from "../../../modules/interfaces/IFightSpiritModule";
import { IView } from "../../mvc/interfaces/IView";
import { FightSpiritModule } from "../../../modules/FightSpiritModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { EResetTimes } from "../../../Message/Public/EResetTimes";
import { TaskInfo } from "../model/TaskInfo";
import { GuideType } from "../rules/GuideType";
import { EquipmentUtil } from "../view/common/util/EquipmentUtil";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { ItemData } from "../resource/info/ItemData";
import { Global } from "../../../com/gengine/global/Global";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { Alert } from "../../../com/mui/controls/Alert";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
type int = number;
//class FightSpiritController
    
    export  class FightSpiritController extends Controller
    {
       

        public get mountModule(): IFightSpiritModule
        {
            if (null == this._fightSpiritModule) 
            {
                this.initView();
            }
            return this._fightSpiritModule;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._fightSpiritModule) 
            {
                this._fightSpiritModule = new FightSpiritModule();
            }
            return this._fightSpiritModule;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateCultureFightSpiritCrit, this.onCultureFightSpiritCrit);
            Dispatcher.addEventListener(EventName.FightSpiritShowModule, this.onFightSpiritShowModule);
            Dispatcher.addEventListener(EventName.FightSpiritLevelChange, this.onFightSpiritLevelChange);
            Dispatcher.addEventListener(EventName.FightSpiritExpChange, this.onFightSpiritExpChange);
            Dispatcher.addEventListener(EventName.FightSpiritGoUpGrade, this.goUpgradeLevel);
            Dispatcher.addEventListener(EventName.FightSpiritLeftTimeChange, this.onFightSpiritLeftTimeChange);
            Dispatcher.addEventListener(EventName.FightSpiritCheckSuitData, this.onFightSpiritCheckSuitData);
            Dispatcher.addEventListener(EventName.FightSpiritGogetBtnTooltips, this.onFightSpiritGogetBtnTooltips);
            Dispatcher.addEventListener(EventName.FightSpiritGogetMisson, this.onFightSpiritGogetMisson);
            Dispatcher.addEventListener(EventName.FightSpiritGoUpdateTimes, this.onFightSpiritGoUpdateTimes);
            Dispatcher.addEventListener(EventName.FightSpiritGoUpdateTimesChange, this.onFightSpiritGoUpdateTimesChange);
            Dispatcher.addEventListener(EventName.FightSpiritGoBreak, this.onFightSpiritGoBreak);
            Dispatcher.addEventListener(EventName.FightSpiritChangePanelUpdateView, this.onFightSpiritChangePanelUpdateView);
            NetDispatcher.addCmdListener(ServerCommand.RolepackDataChange, this.onUpdateView);
            NetDispatcher.addCmdListener(ServerCommand.RolePackItemsChange, this.onUpdateView);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerSecondUpdate, this.onUpdateView);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerEnableUpdate, this.onUpdateView);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onBackPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.VIPInfoUpdateCommand, this.onVipInfoUpdate);
            return;
        }

        /* internal  */onFightSpiritChangePanelUpdateView(arg1: DataEvent=null): void
        {
            if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
            {
                this._fightSpiritModule.changePanel();
                this.onFightSpiritCheckSuitData();
                this.onBackPackChangeHandler();
                this.onFightSpiritLeftTimeChange();
                this.onFightSpiritGoUpdateTimesChange();
            }
            return;
        }

        /* internal  */onFightSpiritGoBreak(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            GameProxy.roleProxy.breakFightSpirit(loc1);
            return;
        }

        /* internal  */onFightSpiritGoUpdateTimesChange(arg1: DataEvent=null): void
        {
            if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
            {
                this._fightSpiritModule.updateResetTime();
            }
            return;
        }

        /* internal  */onFightSpiritGoUpdateTimes(arg1: DataEvent): void
        {
            var loc1=new EResetTimes(EResetTimes._EResetTimesFightSpirit);
            GameProxy.roleProxy.resetTimes(loc1);
            return;
        }

        /* internal  */taskEndHandler(arg1: Object): void
        {
            if (arg1 == null) 
            {
                return;
            }
            var loc1=arg1.task as TaskInfo;
            if (loc1 && !(loc1.sTask == null) && loc1.sTask.guide & GuideType.GuideFightSpirit) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.FightSpiritGuidePlayerBtn));
            }
            return;
        }

        /* internal  */onFightSpiritGogetBtnTooltips(arg1: DataEvent): void
        {
            var loc1=EquipmentUtil.getCurRolePackEquipsData(cache.pack.rolePackCache.getAllItemsEnabledCareer());
            Dispatcher.dispatchEvent(new DataEvent(EventName.FightSpiritUpdateBtnToolTips, loc1));
            return;
        }

        /* internal  */onFightSpiritCheckSuitData(arg1: DataEvent=null): void
        {
            var loc1=EquipmentUtil.getCurRolePackEquipsData(cache.pack.rolePackCache.getAllItemsEnabledCareer());
            Dispatcher.dispatchEvent(new DataEvent(EventName.FightSpiritUpdateBtnToolTips, loc1));
            if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
            {
                this._fightSpiritModule.updateInfo(loc1);
            }
            return;
        }

        /* internal  */onBackPackChangeHandler(arg1: Object=null): void
        {
            if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
            {
                this._fightSpiritModule.checkPropInPack();
            }
            return;
        }

        /* internal  */onCultureFightSpiritCrit(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
            {
                if (loc1.valueStr == "notCrit") 
                {
                    this._fightSpiritModule.addGrowthEffect(loc1.value);
                }
                else 
                {
                    this._fightSpiritModule.addGrowthEffect(loc1.value, true);
                }
            }
            return;
        }

        /* internal  */goUpgradeLevel(arg1: DataEvent): void
        {
            var loc4=null;
            var loc1=arg1.data.propArr as Array<any>;
            var loc2=arg1.data.type as int;
            var loc3=[];
            var loc5=0;
            while (loc5 < loc1.length) 
            {
                if ((loc4 = loc1[loc5] as ItemData).bind > 0) 
                {
                    loc3.unshift(loc4.uid);
                }
                else 
                {
                    loc3.push(loc4.uid);
                }
                ++loc5;
            }
            GameProxy.roleProxy.cultureFightSpirit((loc1[0] as ItemData).posType, loc3, loc2);
            return;
        }

        /* internal  */onUpdateView(arg1: Object=null): void
        {
            this.onFightSpiritCheckSuitData();
            return;
        }

        /* internal  */onFightSpiritShowModule(arg1: DataEvent): void
        {
            if (cache.role.fightSpiritOpenFlag != 1) 
            {
                if (Global.isDebugModle) 
                {
                    this.view.show();
                    this.onFightSpiritChangePanelUpdateView();
                    return;
                }
                MsgManager.showRollTipsMsg(Language.getString(71120));
            }
            else if (this.view && this.view.isHide) 
            {
                this.view.show();
                this.onFightSpiritChangePanelUpdateView();
            }
            else 
            {
                this.view.hide();
            }
            return;
        }

        /* internal  */onFightSpiritLevelChange(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                this.onFightSpiritCheckSuitData();
                if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
                {
                    this._fightSpiritModule.addLevelUpEffect();
                }
            }
            return;
        }

        /* internal  */onFightSpiritExpChange(arg1: DataEvent): void
        {
            this.onFightSpiritCheckSuitData();
            return;
        }

        /* internal  */onFightSpiritLeftTimeChange(arg1: DataEvent=null): void
        {
            if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
            {
                this._fightSpiritModule.timeChange(false);
            }
            return;
        }

        /* internal  */onFightSpiritGogetMisson(arg1: DataEvent): void
        {
            Alert.showCloseBtn = true;
            Alert.okLabel = Language.getString(70432);
            Alert.calcelLabel = Language.getString(70433);
            Alert.buttonWidth = 75;
            Alert.show(Language.getString(71118), null, Alert.OK | Alert.CANCEL, null, this.onAlertClickHandler);
            return;
        }

        /* internal  */onAlertClickHandler(arg1: int): void
        {
            var loc1=cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectCrossDefenceCopy);
            if (loc1) 
            {
                if (arg1 != Alert.OK) 
                {
                    AIManager.onAIControl(loc1, AIType.AI_Convey);
                }
                else 
                {
                    AIManager.onAutoPathAIControl(loc1);
                }
            }
            return;
        }

        /* internal  */onVipInfoUpdate(arg1: Object=null): void
        {
            if (cache.vip.vipInfo) 
            {
                if (this._fightSpiritModule && !this._fightSpiritModule.isHide) 
                {
                    this._fightSpiritModule.timeChange(true);
                }
            }
            return;
        }

        private /* var */_fightSpiritModule: IFightSpiritModule;
    }
